package com.myblog.service;

import com.myblog.Mapper.BlogMapper;
import com.myblog.po.Blog;
import com.myblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    BlogMapper blogMapper;


    @Override
    public List<Blog> listblog() {
        return blogMapper.listblog();
    }

    @Override
    public Long CountBlog() {
        return blogMapper.CountBlog();
    }

    @Override
    public void saveBlog(Blog blog) {

  if(blog.getId() == null){
      blog.setCreateTime(new Date());
    blog.setUpdateTime(new Date());
    blog.setViews(0);
  }else {
      blog.setUpdateTime(new Date());
  }

        blogMapper.saveBlog(blog);
    }


    @Override
    public void DeleteBlog(Long id) {
        blogMapper.DeleteBlog(id);

    }

    @Override
    public void DeleteBlogFK(Long id) {
        blogMapper.DeleteBlogFK(id);
    }

    @Override
    public Blog getBlog(Long id) {
        return blogMapper.getBlog(id);
    }

    @Override
    public void EditBlog(Blog blog) {
        blogMapper.EditBlog(blog);
    }

    @Override
    public List<Blog> searchblog(BlogQuery blog) {
        return blogMapper.searchblog(blog);
    }

    @Override
    public List<Blog> listRecommendBlogTop() {
        return blogMapper.listRecommendBlogTop();
    }

    @Override
    public List<Blog> seekBlog(String query) {
        return blogMapper.seekBlog(query);
    }

    @Override
    public List<Blog> listBlogbyTypeid(BlogQuery blogQuery) {
        return blogMapper.listBlogbyTypeid(blogQuery);
    }

   @Override
    public Map<String, List<Blog>> archivesBlog() {
       List<String> years = blogMapper.findGroupYear();
       Map<String,List<Blog>> maps = new HashMap<>() ;
       for (String year:years){
           maps.put(year,blogMapper.findByYear(year)  );
       }

        return maps;
    }

    @Override
    public void AddViews(Long id) {
        blogMapper.AddViews(id);
    }

}
